package ywpt.web.action.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.JobInfo;
import ywpt.model.JobProcInfo;
import ywpt.service.service.JobInfoService;
import ywpt.service.service.JobProcInfoService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class JobProcInfoAction extends BaseAction implements ModelDriven<JobProcInfo>, Preparable{

	/**
	 * 服务过程Action
	 */
	private static final long serialVersionUID = 9204207388061711553L;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];	
	private Long id;
	private JobProcInfo jobProcInfo;
	private List<JobProcInfo> jobProcInfoList = new ArrayList<JobProcInfo>();
	private JobProcInfoService jobProcInfoService;
	
	private int version;
	
	private Long jobId;
	private Long clientId;
	private Long keshiId; 
	private Long softId;
	private String softName;
	private Long deviceId;
	private String deviceName;
	private Long evaluationId;
	
	private JobInfo jobInfo;
	private JobInfoService jobInfoService;	
	
	public JobInfo getJobInfo() {
		return jobInfo;
	}

	public void setJobInfo(JobInfo jobInfo) {
		this.jobInfo = jobInfo;
	}

	public JobInfoService getJobInfoService() {
		return jobInfoService;
	}

	public void setJobInfoService(JobInfoService jobInfoService) {
		this.jobInfoService = jobInfoService;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public Long getSoftId() {
		return softId;
	}

	public void setSoftId(Long softId) {
		this.softId = softId;
	}

	public Long getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}

	public Long getEvaluationId() {
		return evaluationId;
	}

	public void setEvaluationId(Long evaluationId) {
		this.evaluationId = evaluationId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public JobProcInfo getJobProcInfo() {
		return jobProcInfo;
	}

	public void setJobProcInfo(JobProcInfo jobProcInfo) {
		this.jobProcInfo = jobProcInfo;
	}

	public List<JobProcInfo> getJobProcInfoList() {
		return jobProcInfoList;
	}

	public void setJobProcInfoList(List<JobProcInfo> jobProcInfoList) {
		this.jobProcInfoList = jobProcInfoList;
	}

	public JobProcInfoService getJobProcInfoService() {
		return jobProcInfoService;
	}

	public void setJobProcInfoService(JobProcInfoService jobProcInfoService) {
		this.jobProcInfoService = jobProcInfoService;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	public String getSoftName() {
		return softName;
	}

	public void setSoftName(String softName) {
		this.softName = softName;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}
	/*--------------------------------------------------------------------------------------------------*/
	@Override
	public JobProcInfo getModel() {
		return this.jobProcInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.jobProcInfo = this.jobProcInfoService.getJobProcInfoById(id);
		}else{
			this.jobProcInfo = new JobProcInfo();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		
		if (this.jobId!=null) {
			jobInfo = this.jobInfoService.getJobInfoById(jobId);
			if (jobInfo.getMark()==1) {
				PublicFunction.getOperationPopedom(oper, popedom, OperationConstants.SERVICE_SOFTWARE);	
			} else {
				PublicFunction.getOperationPopedom(oper, popedom, OperationConstants.SERVICE_HARDWARE);	
			}
		}else if (this.jobProcInfo.getJobInfo()!=null) {
			if (this.jobProcInfo.getJobInfo().getMark()==1) {
				PublicFunction.getOperationPopedom(oper, popedom, OperationConstants.SERVICE_SOFTWARE);	
			} else {
				PublicFunction.getOperationPopedom(oper, popedom, OperationConstants.SERVICE_HARDWARE);	
			}
		}
			
	}
	protected void showInfo() {
		if (this.jobProcInfo.getJobInfo()!=null) {
			this.setJobId(this.jobProcInfo.getJobInfo().getId());
		}
		if (this.jobProcInfo.getClientDevice()!=null) {
			this.setDeviceId(this.jobProcInfo.getClientDevice().getId());
			this.setDeviceName(this.jobProcInfo.getClientDevice().getName());
		}
		if (this.jobProcInfo.getClientSoft()!=null) {
			this.setSoftId(this.jobProcInfo.getClientSoft().getId());
			this .setSoftName(this.jobProcInfo.getClientSoft().getSoftName());
		}
		if (this.jobProcInfo.getEvaluation()!=null) {
			this.setEvaluationId(this.jobProcInfo.getEvaluation().getId());
		}
		if(this.jobProcInfo.getJobInfo().getClient()!=null){
			this.clientId = this.jobProcInfo.getJobInfo().getClient().getId();
		}
		
	}
	/*===================================================================================================*/
	public String input() {
		if (this.jobId!=null) {
			this.jobInfo = this.jobInfoService.getJobInfoById(jobId);
			this.jobProcInfo.setServiceDate(jobInfo.getStartDate());
		}
		return INPUT;
	}
	
	public String save() {
		try {
			if(id==null)
			{
				jobProcInfo.setCreateDate(new Date());
				jobProcInfo.setCreater(this.getCurrentUser());
				jobProcInfoService.saveJobProcInfo(jobProcInfo,jobId,softId,deviceId,evaluationId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			}
			else
			{
				if(this.getVersion()==jobProcInfo.getVersion())
				{
					jobProcInfo.setModifyer(this.getCurrentUser());
					jobProcInfo.setModifyDate(new Date());
					jobProcInfoService.updateJobProcInfo(jobProcInfo,jobId,softId,deviceId,evaluationId);
					this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
				}
				else
					this.addActionMessage(SystemMessage.UPDATE_FAIL);
			}
			return SUCCESS;
		} catch (UniqueConstraintException e) {
			this.addActionError(e.getMessage());
			return INPUT;
		}
	}
	public String edit() {
		this.jobProcInfo = this.jobProcInfoService.getJobProcInfoById(id);
		showInfo();
		return INPUT;
	}
	public String delete() {
		if(id!=null){
			jobProcInfoService.removeJobProcInfoById(id);
		}else{
			this.addActionMessage(SystemMessage.NO_OBJECT);
		}
		return SUCCESS;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public Long getKeshiId() {
		return keshiId;
	}

	public void setKeshiId(Long keshiId) {
		this.keshiId = keshiId;
	}

	
	

}
